package org.rcloud.io.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.rcloud.io.Utils.PageVo;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.model.Douyin;
import org.rcloud.io.service.DouyinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@RestController
@RequestMapping(value = "/douyin")
public class DouyinController {


    @Autowired
    private DouyinService ds;

    /*
     * 获取所有的抖音线路信息
     * */
    @ApiOperation(value="获取所有的抖音线路信息", notes="获取所有的抖音线路信息")
    @ResponseBody
    @RequestMapping(value = "/findDouyin", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public PageVo<Douyin> findScenery(){
        return ds.findDouyin();
    }


    /*
     * 根据eid获取抖音线路信息
     * */
    @ApiOperation(value="根据eid获取抖音线路信息", notes="根据eid获取抖音线路信息")
    @ResponseBody
    @RequestMapping(value = "/findByEid", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResultVo findByEid(String eid){
        System.out.println(eid+"++++++++++");
        return ds.findByEid(eid);
    }

    /*
     * 根据id获取抖音线路信息
     * */
    @ApiOperation(value="根据id获取抖音线路信息", notes="根据id获取抖音线路信息")
    @ResponseBody
    @RequestMapping(value = "/findById", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResultVo findById(Integer id){

        return ds.findById(id);
    }


    /*
     * 保存抖音线路信息
     * */
    @ApiOperation(value="保存抖音线路信息", notes="保存抖音线路信息")
    @ResponseBody
    @RequestMapping(value = "/saveDouyin", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResultVo saveDouyin(@RequestBody String params){

        JSONObject object =JSON.parseObject(params);

        Map map = JSON.parseObject(object.toString());
        String eid = map.get("eid").toString();
        String name = map.get("name").toString();
        String address = map.get("address").toString();
        String longitude = map.get("longitude").toString();
        String latitude = map.get("latitude").toString();
        String etype = map.get("etype").toString();
        String summary = map.get("summary").toString();
        String report = map.get("report").toString();
        String username = map.get("username").toString();
        String userphone = map.get("userphone").toString();

        Douyin douyin = new Douyin();
        douyin.setEid(eid);
        douyin.setName(name);
        douyin.setAddress(address);
        douyin.setLongitude(longitude);
        douyin.setLatitude(latitude);
        douyin.setEtype(etype);
        douyin.setSummary(summary);
        douyin.setReport(report);
        douyin.setUsername(username);
        douyin.setUserphone(userphone);
        douyin.setFlag(1);

        return ds.saveDouyin(douyin);
    }

    /*
     * 修改抖音线路信息
     * */
    @ApiOperation(value="修改抖音线路信息", notes="修改抖音线路信息")
    @ResponseBody
    @RequestMapping(value = "/updateDouyin", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResultVo updateScenery(@RequestBody String params){
        JSONObject object =JSON.parseObject(params);
        Map map = JSON.parseObject(object.toString());

        int id = Integer.parseInt(map.get("id").toString());
        String eid = map.get("eid").toString();
        String name = map.get("name").toString();
        String address = map.get("address").toString();
        String longitude = map.get("longitude").toString();
        String latitude = map.get("latitude").toString();
        String etype = map.get("etype").toString();
        String summary = map.get("summary").toString();
        String report = map.get("report").toString();
        String username = map.get("username").toString();
        String userphone = map.get("userphone").toString();

        Douyin douyin = new Douyin();
        douyin.setId(id);
        douyin.setEid(eid);
        douyin.setName(name);
        douyin.setAddress(address);
        douyin.setLongitude(longitude);
        douyin.setLatitude(latitude);
        douyin.setEtype(etype);
        douyin.setSummary(summary);
        douyin.setReport(report);
        douyin.setUsername(username);
        douyin.setUserphone(userphone);

        return ds.updateDouyin(douyin);
    }

}
